package com.wz.jiangsu.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: wz
 * @author: wangzhou
 * @create: 2024-02-01 10:43
 * @description:
 **/
@Component
public class IsAnnotationInterceptor implements HandlerInterceptor {


    // 在业务处理器处理请求之前被调用
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle");
        return true;
        // 获取请求的接口路径数据
//        String contentType = request.getContentType();
//        System.out.println("contentType = " + contentType);
//        String requestURL = request.getRequestURL().toString();  // 完整的 URL
//        String requestURI = request.getRequestURI();  // 请求的 URI (不包含主机名)
//        String contextPath = request.getContextPath();  // 应用的上下文路径
//        String servletPath = request.getServletPath();  // Servlet 的路径
//
//        ServletInputStream inputStream = request.getInputStream();
//
//        // 使用 ByteArrayOutputStream 读取 InputStream 数据
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];
//        int length;
//
//        while ((length = inputStream.read(buffer)) != -1) {
//            byteArrayOutputStream.write(buffer, 0, length);
//        }
//
//        // 将 ByteArrayOutputStream 转换为字节数组
//        byte[] data = byteArrayOutputStream.toByteArray();
//
//        // 处理字节数据
//        System.out.println("信息：" + new String(data, "UTF-8"));
//
////        byteArrayOutputStream.close();
////        inputStream.close();
//
//        // 打印路径信息
//        System.out.println("Request URL: " + requestURL);
//        System.out.println("Request URI: " + requestURI);
//        System.out.println("Context Path: " + contextPath);
//        System.out.println("Servlet Path: " + servletPath);
//        if (handler instanceof HandlerMethod) {
//            HandlerMethod handlerMethod = (HandlerMethod) handler;
//            Method method = handlerMethod.getMethod();
//            TestAnnotation annotation = method.getAnnotation(TestAnnotation.class);
//            if (annotation != null) {
//                String value = annotation.value();
//                System.out.println(value);
//                return true;
//            }
//
//        }
//        return true;
    }
    // 在业务处理器处理请求完成之后，生成视图之前执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
//        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
    // 在DispatcherServlet完全处理完请求之后被调用，可用于清理资源
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
